package com.cl.edu.service.teacher;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cl.core.orm.Page;
import com.cl.edu.dao.BookingstudentDAO;
import com.cl.edu.dao.TeacherDAO;
import com.cl.edu.entity.Bookingstudent;
import com.cl.edu.entity.Teacher;

@Service
@Transactional
public class TeacherManager {
	@Autowired
	private TeacherDAO teacherDAO;
	 
	@Autowired
	private BookingstudentDAO bookingstudentDAO;
	
	/**
	 * 根据id得到教员信息
	 * @param id
	 * @return
	 */
	public Teacher getTeacher(Long id) {
		return teacherDAO.get(id);
	}
	
	public void updateTeacher(Teacher entity){
		teacherDAO.saveOrUpdate(entity);
	}
	
	/**
	 * 是否存在用户名
	 * @param teacher
	 * @return
	 */
	public boolean hasUsername(Teacher teacher) {
		List<Teacher> list = teacherDAO.findBy("username",
				teacher.getUsername());
		return list.size() > 0;
	}

	/**
	 * 指定教师是否存在
	 * @param teacher
	 * @return
	 */
	public Teacher isExist(Teacher teacher) {
		StringBuffer hql = new StringBuffer("from Teacher model");
		hql.append(" where 1=1 ");
		hql.append(" and model.username = '").append(teacher.getUsername())
				.append("' and model.password = '").append(teacher.getPassword())
				.append("'");
		List<Teacher> list = teacherDAO.find(hql.toString());
		if (list.size() > 0) {
			return list.get(0);
		}
		return null;
	}
	
	/**
	 * 由教员id查询预约的学员记录
	 * @param id
	 * @return
	 */
	public Page<Bookingstudent> getBookingstudents(Page<Bookingstudent> page,Long id) {
		StringBuffer hql = new StringBuffer("from Bookingstudent model");
		hql.append(" where model.teacher.id=").append(id);
		hql.append(" order by model.").append(page.getOrderBy()).append(" ").append(page.getOrder());
		page = bookingstudentDAO.findPage(page, hql.toString());
		return page;
	}
	
	public void evict(Teacher teacher) {
		teacherDAO.getSession().evict(teacher);
	}
}
